package com.shimao.celnet.payment.controller;

import com.shimao.celnet.payment.form.BCityForm;
import com.shimao.celnet.payment.model.BCity;
import com.shimao.celnet.payment.service.BCityService;
import com.shimao.celnet.payment.util.FinalUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 城市
 * Created by ensure on 2017/12/04.
 *
 */
@Controller
@RequestMapping("/citys")
public class BCityController {
    protected  Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private BCityService localService;


    @RequestMapping("/list")
    public String getAll(@ModelAttribute BCityForm formBean, String pageActionNm, Model model){
        logger.info("查询列表页面--开始");
        if(StringUtils.isNotEmpty(formBean.getGuid())){
            //更新，新增后，重新刷新情况，查询条件清空
            formBean=new BCityForm();
        }

        //当前页码设定
        int pageNum=FinalUtil.PAGE_Fist_NUM;

        if(formBean.isIsLastPage()){
            pageNum=formBean.getPages();
        }

        if(!StringUtils.isEmpty(pageActionNm) && !"0".equals(pageActionNm)){
            pageNum=Integer.valueOf(pageActionNm);
        }

        //页面数据查询结果
        formBean = localService.getByPage(formBean, pageNum, FinalUtil.PAGE_LENGHT);

        model.addAttribute("formBean", formBean);

        logger.info("查询列表页面--结束");

        return "city/cityList";
    }


    /**
     * 更新初始化
     * @param
     * @return
     */
    @RequestMapping("/addInfo")
    public @ResponseBody BCity addInfo() {
        logger.info("新增页面--初始化");
        BCity addBean = new BCity();

        return addBean;
    }

    /**
     * 更新初始化
     * @param
     * @return
     */
    @RequestMapping("/editInfo")
    public @ResponseBody BCity editInfo(@RequestBody  String guid) {
        logger.info("修改页面--初始化");
        BCity editBean = new BCity();
        if(StringUtils.isNotEmpty(guid)) {
            //编辑数据取得
            editBean = localService.getByKey(guid);

        }
        return editBean;
    }


    /**
     * 新增保存
     * @param
     * @return
     */
    @RequestMapping("/addSave")
    public  @ResponseBody Map<String, Object> addSave(@RequestBody @Valid BCity addForm,BindingResult result) {
        logger.info("新增提交--开始");
        //返回map
        Map<String, Object> returnMap = new HashMap<String, Object>();

        //界面输入验证
        if (result.hasErrors()) {
            for(FieldError error:result.getFieldErrors()) {
                returnMap.put(error.getField() + FinalUtil.VALID_MSG, error.getDefaultMessage());
                //验证错误==2
                returnMap.put(FinalUtil.ERROR_CODE,  FinalUtil.SAVE_ERR_2);
            }
            logger.debug("新增提交--页面验证出错");
            return returnMap;
        }

        try{
            logger.debug("新增提交--数据保存");
            //数据新增，GUID取得
            localService.save(addForm);

            //新增成功消息==0
            returnMap.put(FinalUtil.ERROR_CODE,  FinalUtil.SAVE_SCU_0);
            logger.debug(FinalUtil.PAGE_ADD_SCU);

        } catch (Exception e) {
            //新增失败消息==1
            returnMap.put(FinalUtil.ERROR_CODE,  FinalUtil.SAVE_ERR_1);
            returnMap.put(FinalUtil.ERROR_MSG,  FinalUtil.PAGE_ADD_ERR);
            e.printStackTrace();
            logger.error(FinalUtil.PAGE_ADD_ERR);
            logger.error(e);
        }
        logger.info("新增提交--结束");
        return returnMap;
    }

    /**
     * 更新保存
     * @param
     * @return
     */
    @RequestMapping("/editSave")
    public  @ResponseBody Map<String, Object> editSave(@RequestBody @Valid BCity editForm, BindingResult result) {
        logger.info("修改提交--开始");
        //返回map
        Map<String, Object> returnMap = new HashMap<String, Object>();

        //界面输入验证
        if (result.hasErrors()) {
            for(FieldError error:result.getFieldErrors()) {
                returnMap.put(error.getField() + FinalUtil.VALID_MSG, error.getDefaultMessage());
                //验证错误==2
                returnMap.put(FinalUtil.ERROR_CODE,  FinalUtil.SAVE_ERR_2);
            }
            logger.debug("修改提交--页面验证出错");
            return returnMap;
        }

        String loginUserGuid = (String) SecurityUtils.getSubject().getSession().getAttribute("userSessionId");
        editForm.setUpdateUserGuid(loginUserGuid);

        try{
            logger.debug("修改提交--数据保存");
            editForm.setCreatedUserGuid(loginUserGuid);
            localService.update(editForm);

            //成功消息==0
            returnMap.put(FinalUtil.ERROR_CODE,  FinalUtil.SAVE_SCU_0);
            logger.debug(FinalUtil.PAGE_UPD_SCU);
        } catch (Exception e) {
            //失败消息
            returnMap.put(FinalUtil.ERROR_CODE,  FinalUtil.SAVE_ERR_1);
            returnMap.put(FinalUtil.ERROR_MSG,  FinalUtil.PAGE_UPD_ERR);
            e.printStackTrace();
            logger.error(FinalUtil.PAGE_UPD_ERR);
            logger.error(e);
        }
        logger.info("修改提交--结束");
        return returnMap;
    }

    /**
     * 删除
     * @param recordGuid
     * @return
     */
    @RequestMapping("/delete")
    public @ResponseBody Map<String, Object> delete(@RequestBody String recordGuid){
        logger.debug("删除提交--开始");
        //返回map
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            //删除
            int renCnt = localService.delete(recordGuid);
            if(renCnt<=0){
                returnMap.put(FinalUtil.ERROR_CODE,  FinalUtil.SAVE_ERR_1);
                returnMap.put(FinalUtil.ERROR_MSG,  FinalUtil.PAGE_DEL_ERR);
                return returnMap;
            }

            //成功消息==0
            returnMap.put(FinalUtil.ERROR_CODE,  FinalUtil.SAVE_SCU_0);
            logger.debug(FinalUtil.PAGE_DEL_SCU);
        } catch (Exception e) {
            //失败消息
            returnMap.put(FinalUtil.ERROR_CODE,  FinalUtil.SAVE_ERR_1);
            returnMap.put(FinalUtil.ERROR_MSG,  FinalUtil.PAGE_DEL_ERR);
            e.printStackTrace();
            logger.error(FinalUtil.PAGE_DEL_ERR);
            logger.error(e);
        }
        logger.debug("删除提交--结束");
        return returnMap;
    }

}
